import { SortValue } from './data';

/* 设定无序列表默认index为排序值, startIndex为起始排序值 */
export function listInitSort<T>(list: T[], startIndex = 1) {
  return list.map((item, index) => ({ ...item, sort: startIndex + index }));
}

/**
 * @param index 当前索引
 * @param list 当前列表
 * @returns back 是否能后退，forward是否能前进
 */
export function canMove(index: number, list: any[]) {
  return {
    back: !(index === list.length - 1),
    forward: !(index === 0),
  };
}

/**
 * @param options
 * @returns 根据位置信息和action找到置换sort值
 */
export function getSortValue<T extends SortValue>(options: {
  action: 'forward' | 'back' | 'first' | 'last' | 'delete';
  list: T[];
  index: number;
}) {
  let startSort = 0;
  let endSort = 0;
  const { action, index, list } = options;
  // 在第一个位置并且向前，退出
  if (index === 0 && action === 'forward') return;
  // 在最后一个位置并且向后，退出
  if (index === list.length - 1 && action === 'back') return;
  startSort = list[index].sort;

  switch (action) {
    case 'forward': {
      endSort = list[index - 1].sort;
      break;
    }
    case 'back': {
      endSort = list[index + 1].sort;
      break;
    }
    case 'first': {
      endSort = list[0].sort - 1;
      break;
    }
    case 'last': {
      endSort = list[list.length - 1].sort + 1;
      break;
    }
  }
  return {
    startSort,
    endSort,
  };
}

/**
 *
 * @param options
 * @returns  根据换位sort，排序得到新的有序列表
 */
export function moveSort<T extends SortValue>(options: {
  startSort: number;
  endSort: number;
  list: T[];
}) {
  const { startSort, endSort, list } = options;
  const startSortIndex = list.findIndex((item) => item.sort === startSort);
  // 没有移动 | 开始位置不存在，退出
  if (startSort === endSort || startSortIndex === -1) return list;
  const endSortIndex = list.findIndex((item) => item.sort === endSort);
  // 如果最终位置不存在，那么设置当前位置为最终位置
  const copyList = [...list];
  copyList[startSortIndex] = {
    ...copyList[startSortIndex],
    sort: endSort,
  };
  if (copyList[endSortIndex]) {
    copyList[endSortIndex] = {
      ...copyList[endSortIndex],
      sort: startSort,
    };
  }
  copyList.sort((prev, next) => prev.sort - next.sort);
  return copyList;
}

export type IDealAction = 'forward' | 'back' | 'first' | 'last' | 'delete';

/**
 *
 * @param options
 * @returns 通用的处理排序的方法
 */
export function sortAction<T extends SortValue>(
  options:
    | {
        action: IDealAction;
        list: T[];
        index: number;
      }
    | {
        startSort: number;
        endSort: number;
        list: T[];
      },
) {
  const { list } = options;

  let startSort = 0;
  let endSort = 0;
  if ('action' in options) {
    const { action, index } = options;
    const result = getSortValue({ action, index, list });
    // 至少保证重新排序
    if (!result) return listInitSort(list);
    startSort = result.startSort;
    endSort = result.endSort;
  } else {
    startSort = options.startSort;
    endSort = options.endSort;
  }
  // 重新排序
  const newList = listInitSort(moveSort({ startSort, endSort, list }));
  return newList;
}
